import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { FetchService } from '../../../../core/net/fetch.service';
import { map } from 'rxjs/operators';

@Component({
    selector: 'app-pm-flood-material',
    templateUrl: './pm-flood-material.component.html',
    styleUrls: ['./pm-flood-material.component.scss']
})
export class PmFloodMaterialComponent implements OnInit {
    materialTypeList: Array<{ label: string, value: string | number }>;
    statusTypeList: Array<{ label: string, value: string | number }>;
    materialMapper;
    statusMapper;
    searchForm: FormGroup;
    tableData: Array<any> = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = false;
    form: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    queryType: "";

    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            suppliesType: [null],
            strorageLocation: [null],
            suppliesStatus: [null]
        });
        this.form = this.fb.group({
            id: [null],
            suppliesCode: [null, Validators.required],
            suppliesName: [null, Validators.required],
            suppliesType: [null, Validators.required],
            strorageLocation: [null, Validators.required],
            suppliesInfo: [null, Validators.required],
            contact: [null, Validators.required],
            suppliesStatus: [null, Validators.required],
            employer: [null, Validators.required],
            remarks: [null, Validators.required]
        });
    }

    ngOnInit(): void {
        this.getDictValue('10015').subscribe(res => {
            this.materialTypeList = res.list;
            this.materialMapper = res.mapper;
        });
        this.getDictValue('10016').subscribe(res => {
            this.statusTypeList = res.list;
            this.statusMapper = res.mapper;
        });
        this.queryInfo();
    }

    getDictValue(typeId) {
        return this.fetchService
            .post<any>('sys/sysDictValue/list', {
                dictTypeId: typeId
            })
            .pipe(
                map(res => {
                    const list = res['list'] || [];
                    const mapper = list.reduce((def, item) => {
                        def[item.value] = item.label;
                        return def;
                    }, {});
                    return {
                        list,
                        mapper
                    };
                })
            );
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            ...this.searchForm.value,
            pageNum: this.pageNum,
            pageSize: this.pageSize,
        };
        this.fetchService.post('projectManage/wrsSuppliesB/list', params).subscribe(res => {
            this.tableData = res['list'];
            this.total = res['total'];
            this.loading = false;
        });
    }

    openEditModal(isEditable: boolean = true, item?): void {
        this.modalTitle = isEditable ? (item ? `编辑` : '新增') : '详情';
        this.queryType = isEditable ? "" : null;
        if (!!item) {
            item['suppliesStatus'] = item['suppliesStatus'] + '';
            item['suppliesType'] = item['suppliesType'] + '';
        }
        this.form.patchValue(item || {});
        if (isEditable) {
            this.form.enable();
        } else {
            this.form.disable();
        }
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.get(`projectManage/wrsSuppliesB/remove/${id}`).subscribe(resp => {
        this.message.info('删除成功');
        this.queryInfo();
        });
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = this.form.value;
        const url = data.id ? 'projectManage/wrsSuppliesB/edit' : 'projectManage/wrsSuppliesB/add';
        this.fetchService.post(url, data).subscribe(res => {
            this.message.success('提交成功');
            this.modalShow = false;
            this.queryInfo();
            this.form.reset();
        });
    }

    onCancel() {
        this.modalShow = false;
        this.form.reset();
    }
}
